/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package id3tagger.db;

import java.util.ArrayList;

/**
 *
 * @author monzie
 */
public abstract class QueryGenerator
{
    public static String search(String artiste, String album, String genre) throws Exception
    {
        String query = "SELECT * FROM SongDB WHERE";
        int count = 0;

        if(artiste == null && album == null && genre == null) throw new Exception("Requête incorrecte.");
        if(artiste != null)
        {
            query+="artiste="+artiste+"'";
            ++count;
        }

        if(album != null)
        {
            if(count != 0) query+=" AND ";
            query+="album="+album+"'";
            ++count;
        }

        if(genre != null)
        {
            if(count != 0) query+=" AND ";
            query+="genre='"+genre+"'";
        }

        query+=";";

        return query;
    }

    public static String genQuery(ArrayList <String> genres, ArrayList <String> artistes, ArrayList <String> albums)
    {
        int ops = 0;
        boolean where = false;
        String query="SELECT * FROM SONGS ";

        if((genres.size() != 0))
        {
            query+="WHERE (";
            where = true;
            for(int i = 0; i < genres.size(); ++i)
            {
                if(i >= 1 && i <= (genres.size() -1))
                    query+=" OR ";
                query+="genre='"+genres.get(i).replace("'", "''")+"'";
            }
            query+=") ";
            ++ops;
        }

        if(artistes.size() != 0)
        {
            if(ops > 0)
            {
                query+=" AND (";
                ops = 1;
            }
            
            else if (where ==false)
            {
                query+="WHERE (";
                where = true;
                ++ops;
            }
            
            for(int i = 0; i < artistes.size(); ++i)
            {
                if(i >= 1 && i <= (artistes.size() -1))
                    query+=" OR ";
                query+="artiste='"+artistes.get(i).replace("'", "''")+"'";
            }
            query+=") ";
        }

        if(albums.size() != 0)
        {
            if(ops > 0)
            {
                query+=" AND (";
                ops = 1;
            }
            
            else if (where == false)
            {
                query+="WHERE (";
                where = true;
                ++ops;
            }

            for(int i = 0; i < albums.size(); ++i)
            {
                if(i >= 1 && i <= (albums.size() -1))
                    query+=" OR ";
                query+="album='"+albums.get(i).replace("'", "''")+"'";
            }
            query+=") ";
        }

        query+=";";

        return query;
    }

    /**
     * Liste tous les éléments d'une catégorie (artiste, genre...). Ne renvoie qu'une seule occurence
     * @param s
     * @return Requête de listing
     */
    
    public static String list(String s, String where)
    {
        String query = "SELECT DISTINCT "+s+" FROM Songs";
        if(where != null)
            query+=" WHERE "+s+"='"+where.replace("'", "''")+"'";
        query+=";";
        return query;
    }
}